package com.ruoyi.inspection.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.inspection.domain.GovernCheckTaskPlan;
import com.ruoyi.inspection.domain.GovernCheckTaskPlanItem;
import com.ruoyi.inspection.domain.vo.GovernCheckTaskPlanVo;
import com.ruoyi.inspection.service.GovernCheckTaskPlanItemService;
import com.ruoyi.inspection.service.GovernCheckTaskPlanService;
import com.ruoyi.inspection.service.GovernTaskService;
import com.ruoyi.inspection.utils.IdFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 政府专项检查计划表(GovernCheckTaskPlan)表控制层
 *
 * @author makejava
 * @since 2024-06-19 16:16:09
 */
@RestController
@Api("任务计划相关接口")
@RequestMapping("/check/governCheckTaskPlan")
public class GovernCheckTaskPlanController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private GovernCheckTaskPlanService governCheckTaskPlanService;
    @Resource
    private GovernTaskService taskService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     *
     * @param governCheckTaskPlan 筛选条件
     * @return 查询结果
     */
    @ApiOperation("检查计划列表")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:list')")
    @GetMapping("/list")
    public TableDataInfo queryByPage(GovernCheckTaskPlan governCheckTaskPlan) {
        startPage();
        List<GovernCheckTaskPlan> governCheckTaskPlans = this.governCheckTaskPlanService.queryByPage(governCheckTaskPlan);
        return getDataTable(governCheckTaskPlans);
    }

    /**
     * 检查个人记录
     *
     * @param governCheckTaskPlan 筛选条件
     * @return 查询结果
     */
    @ApiOperation("检查个人记录")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:list')")
    @GetMapping("/userSelf/list")
    public TableDataInfo queryByCheckUser(HttpServletRequest request, GovernCheckTaskPlan governCheckTaskPlan) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governCheckTaskPlan.setCheckUserId(user.getUserId());
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        if (getUserId()!=1 && !zxgly) {
            governCheckTaskPlan.setCheckUserId(getUserId() + "");
        }
        startPage();
        List<GovernCheckTaskPlan> governCheckTaskPlans = this.governCheckTaskPlanService.selectByCheckUser(governCheckTaskPlan);
        return getDataTable(governCheckTaskPlans);
    }

    @ApiOperation("检查个人记录")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:list')")
    @GetMapping("/api/userSelf/list")
    public TableDataInfo queryUsersTaskList(HttpServletRequest request, GovernCheckTaskPlan governCheckTaskPlan) {
//        governCheckTaskPlan.setCheckUserId(SecurityUtils.getUserId());
//        LoginUser user = tokenService.getLoginUser(request);
//        governCheckTaskPlan.setCheckUserId(user.getUserId());
        boolean zxgly = SecurityUtils.getLoginUser().getUser().getRoles().stream().map(SysRole::getRoleId).distinct().collect(Collectors.toList()).contains(InspectionConstant.ROLE_ZXGL_ID);
        if (getUserId()!=1 && !zxgly) {
            governCheckTaskPlan.setCheckUserId(getUserId() + "");
        }
        startPage();
        List<GovernCheckTaskPlanVo> governCheckTaskPlans = this.governCheckTaskPlanService.selectUsersTaskList(governCheckTaskPlan);
        return getDataTable(governCheckTaskPlans);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param governTaskPlanId 主键
     * @return 单条数据
     */
    @ApiOperation("查看检查计划")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:query')")
    @GetMapping("/{governTaskPlanId}")
    public AjaxResult queryById(@PathVariable("governTaskPlanId") Long governTaskPlanId) {
        return AjaxResult.success(this.governCheckTaskPlanService.queryById(governTaskPlanId));
    }

    /**
     * 修改检查计划
     *
     * @param governCheckTaskPlan 实体
     * @return 编辑结果
     */
    @ApiOperation("修改检查计划")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GovernCheckTaskPlan governCheckTaskPlan) {
        governCheckTaskPlan.setGovernUpdateBy(getUsername());

        return toAjax(this.governCheckTaskPlanService.updateTaskInfo(governCheckTaskPlan));
    }

    /**
     * 删除数据
     *
     * @param governTaskPlanId 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除检查计划")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:remove')")
    @DeleteMapping("/{governTaskPlanId}")
    public AjaxResult deleteById(@PathVariable Long governTaskPlanId) {
        return AjaxResult.success(this.governCheckTaskPlanService.deleteById(governTaskPlanId));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param governTaskPlanId 主键
     * @return 单条数据
     */
    @ApiOperation("查询分配人员")
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:query')")
    @GetMapping("/checkUsers/{governTaskPlanId}")
    public AjaxResult queryCheckUsers(@PathVariable("governTaskPlanId") Long governTaskPlanId) {
        return AjaxResult.success(this.governCheckTaskPlanService.queryCheckUsers(governTaskPlanId));
    }

    /**
     * 新增数据
     *
     * @param governCheckTaskPlan 实体
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("@ss.hasPermi('check:governCheckTaskPlan:add')")
    public AjaxResult add(@RequestBody GovernCheckTaskPlan governCheckTaskPlan) {
        LoginUser user = SecurityUtils.getLoginUser();
        governCheckTaskPlan.setGovernCreateBy(user.getUsername());
        return AjaxResult.success(this.governCheckTaskPlanService.insert(governCheckTaskPlan));
    }

//    /**
//     * 编辑计划
//     * @param plan
//     * @return
//     */
//    @PostMapping("/editPlan")
//    public AjaxResult editPlan(GovernCheckTaskPlan plan) {
//
//        return AjaxResult.success(this.taskService.editPlan(plan));
//    }

//    /**
//     * 任务分解生成计划
//     * @param plan
//     * @return
//     */
//    @ApiOperation("任务分解生成计划")
//    @PostMapping("/createPlan")
//    public AjaxResult createPlan(GovernCheckTaskPlan plan) {
//
//        return AjaxResult.success(this.taskService.savePlan(plan));
//    }


//    public static void main(String[] args) throws Exception {
//        GovernCheckTaskPlan plan = new GovernCheckTaskPlan();
//        plan.setId(IdFactory.newLongId());
//        plan.setGovernPlanId(IdFactory.genShortCode());
//        plan.setGovernCheckTaskId(IdFactory.genShortCode());
//        plan.setGovernCheckLevel("一级");
//        plan.setGovernCheckComplete("上海电力");
//        plan.setGovernCheckTaskName("2024危险化学品危险源企业安全专项检查督导");
//        plan.setGovernTaskEndTime("2024-11-01 00:00:00");
//        plan.setGovernTaskStartTime("2024-04-01 00:00:00");
//        plan.setGovernTaskRegisterTime("2024-04-01 00:00:00");
//        plan.setGovernTaskTypeName("危险化学品重大危险源企业安全专项检查督导");
//        plan.setGovernCheckTaskTypeName("企业自查");
//        plan.setGovernInvolveDanger("35KV总降");
//        plan.setGovernCheckComplete("上海电力");
//        plan.setGovernCheckSource("35KV总降");
//        plan.setGovernCheckLevel("一级");
//        plan.setGovernCheckUserNames("李辉,顾治平");
//        plan.setGovernCheckUserIds("3,4");
//        plan.setGovernPlanStatus(1);
//        plan.setGovernCheckVerify(1);
//        governCheckTaskPlanService.insert(plan);
//
//        List<GovernCheckTaskPlanItem> itemList = new ArrayList<>();
//        GovernCheckTaskPlanItem planItem = new GovernCheckTaskPlanItem();
//        planItem.setGovernCheckBasis("《应急管理部办公厅关于印发危险化学品企业重大危险源安全包保责任制办法（试行）的通知》（应急厅〔2021〕12号）第三条");
//        planItem.setGovernApplicablePlace("储存单元、生产单元");
//        planItem.setGovernCheckWay("查台账、查记录");
//        planItem.setGovernCheckContent("明确每一处重大危险源的主要负责人、技术负责人和操作负责人");
//        planItem.setGovernCheckName("1.1");
//        planItem.setGovernCheckUserId("3");
//        planItem.setGovernCheckUserName("李辉");
//        planItem.setGovernCheckTime("2024-04-01 10:00:00");
//        planItem.setGovernPlanId(plan.getGovernPlanId());
//        planItem.setGovernCheckTaskItemId(planItem.getGovernCheckTaskItemId());
//        planItem.setGovernCheckTaskId(planItem.getGovernCheckTaskId());
//        itemList.add(planItem);
//
//        plan.setGovernCheckTaskPlanItemList(itemList);
//        governCheckTaskPlanItemService.insert(planItem);
//    }

}

